//
//  Extension+UIApplication.swift
//  IMKeyBoard
//
//  Created by 汪志浩 on 2022/10/13.
//

import Foundation
import UIKit

/**确保在主线程执行,但是当前如果是主线程就减少不必要的线程切换的开支**/
public func MainThread(_ block:@escaping ()->()){
    if Thread.current.isMainThread {
        block()
    } else {
        DispatchQueue.main.async {
            block()
        }
    }
}

/**
 当前最顶层的控制器
 */
public var k_currentVC : UIViewController {
    get {
        if let rootVC = k_currentWindow?.rootViewController {
            return UIViewController.k_currentViewController(from: rootVC)
        } else {
            return UIViewController()
        }
    }
}

/**
 当前显示的 window
 */
public var k_currentWindow : UIWindow? {
    get{
        if #available(iOS 13.0, *) {
            if let window = UIApplication.shared.delegate?.window {
          return window
         }else{
          return nil
         }
        }else{

         if let window = UIApplication.shared.delegate?.window {

          return window

         }else{

          return nil

         }

        }
    }
}

/**
 当前安全区域的底部高度
 */
public var k_safeAreaInsets_Bottom : CGFloat {
    get{
        guard let window = k_currentWindow else {return 0}
        return window.safeAreaInsets.bottom
    }
}

// MARK: 屏幕尺寸
public let kScreenWidth = UIScreen.main.bounds.size.width
public let kScreenHeight = UIScreen.main.bounds.size.height
public let stateHeight = UIApplication.shared.statusBarFrame.height
/// 底部导航栏高度
public let k_tabBarHeight = 49.0
/// 底部导航栏高度（包括安全区）
public let k_tabBarFullHeight = k_tabBarHeight + k_safeAreaInsets_Bottom
// MARK: 分类方法
public extension UIApplication {
     func exitApp(){
        perform(#selector(NSXPCConnection.suspend))
    }

}
